package cn.com.xy.sms.sdk.ui.simplebubbleview;

import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import cn.com.xy.sms.sdk.ui.popu.simplepart.SimpleBubbleBottom;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import cn.com.xy.sms.util.ParseBubbleManager;
import cn.com.xy.sms.util.SdkCallBack;

/**
 * 
 * @author Administrator
 * 
 */
@SuppressLint("NewApi")
public class DuoquSimpleBubbleViewManager {
    public static final byte DUOQU_RETURN_CACHE_SDK_MSG_ID = 1;
    public static final byte DUOQU_RETURN_CACHE_SDK_MSG_VALUE = 2;
    public final static String TAG = "DuoquSimpleBubbleViewManager";



    public static View getBubbleView(Activity ctx, String msgIds, String phoneNum, String smsCenterNum,
            String smsContent, long smsReceiveTime, byte returnCacheType, ViewGroup parentView,
            HashMap<String, Object> extend) {
        try {
            JSONObject jsonObject = ParseBubbleManager.queryDataByMsgItem(msgIds, phoneNum, smsContent, smsCenterNum, 1,
                    smsReceiveTime);
            if (jsonObject == null) {
                return null;
            }
            JSONArray cacheValue = null;
            try {
                cacheValue = jsonObject.getJSONArray("session_reuslt");
            } catch (Throwable e) {
                SmartSmsSdkUtil.smartSdkExceptionLog("DuoquSimpleBubbleViewManager getBubbleView error:", e);
            }
            if (cacheValue == null) {
                return null;
            }
            return getSimpleBubbleView(ctx, cacheValue, parentView, extend);
        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("DuoquSimpleBubbleViewManager getBubbleView error:", e);
        }
        return null;
    }

    /**
     * @param ctx
     * @param msg
     * @param viewId
     * @return
     */
    public static View getSimpleBubbleView(Activity ctx, JSONArray jsonArray, ViewGroup buttonGroup,
            HashMap<String, Object> extend) throws Exception {
        if (jsonArray == null || buttonGroup == null)
            return null;
        SimpleBubbleBottom view = null;
        if (buttonGroup.getChildCount() > 0) {
            view = (SimpleBubbleBottom) buttonGroup.getChildAt(0);
        }

        if (view != null) {
            try {
                view.setContent(jsonArray, extend);
            } catch (Throwable e) {
                view = null;
            }
        }
        if (view == null) {
            view = new SimpleBubbleBottom(ctx, jsonArray, extend);
            view.setId(Integer.MAX_VALUE);
            buttonGroup.addView(view);
        }
        return view;
    }

    /* linweijie 2017-03-07 15-12 start */
    public static SimpleBubbleBottom getSimpleBubbleView(Activity ctx, JSONArray jsonArray,
            HashMap<String, Object> extend) {
        try {
            SimpleBubbleBottom view = new SimpleBubbleBottom(ctx, jsonArray, extend);
            return view;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    /* linweijie 2017-03-07 15-12 end */

    /**
     * 提供出去的简单气泡的分组方法
     * 
     * @param jsonArray
     *            原按钮数据结果集
     * @param dataSize
     *            需要将按钮分几组
     * @return 分组后的List<JSONArray>, 若解析失败或报错， 则返回null
     */
    public static List<JSONArray> groupSimpleBubbleData(JSONArray jsonArray, int dataSize) {
        List<JSONArray> arrays = null;
        try {
            arrays = ContentUtil.getSimpleBubbleData(jsonArray, dataSize);
        } catch (JSONException e) {
            SmartSmsSdkUtil.smartSdkExceptionLog(TAG, e);
        }
        return arrays;
    }
}
